#include "histogramGray.h"

HistogramGray::HistogramGray(): Histogram()
{	
    histogram=new unsigned int[256];
    CDF= new unsigned long[256];

}

HistogramGray::HistogramGray(QImage* mi): Histogram(mi)
{
    histogram=new unsigned int[256];
    CDF= new unsigned long[256];

}
//HistogramGray::HistogramGray(LImage image):Histogram(image.getImage())
//{
    //HistogramGray::HistogramGray(image.getImage());
//}
HistogramGray ::HistogramGray(QImage qImage):Histogram(&qImage)
{
    this->histogram= new unsigned int[256];
    CDF= new unsigned long[256];

}

HistogramGray::~HistogramGray()
{
    if(histogram!=NULL)
    delete []histogram;
    if(CDF!=NULL)
    delete []CDF;
}

void HistogramGray::calcHistogram()
{
    if(ownerImage==NULL)
    {
        return;
    }
    else
    {
        for(int i=0;i<256;i++)
        {
            histogram[i]=0;
        }
        for(int x=0;x<ownerImage->width();x++)
            for(int y=0;y<ownerImage->height();y++)
                histogram[qGray(ownerImage->pixel(x,y))]++;
       // calcHistImage();
    }
}

QImage HistogramGray::calcHistImage()
{

    int width=512;
    int height=300;
    int fracc=width/256;

    QImage histImage(width,height,QImage::Format_RGB32);
    unsigned long max=0;
    for(int i=0;i<256;i++)
        if(histogram[i]>max)
            max=histogram[i];
    for(int x=0;x<width;x++)
        for(int y=0;y<height;y++)
            histImage.setPixel(x,y,qRgb(0,0,0));
    for(int i=0;i<width;i++)
    {
        int h=(height-1)*(1.0-histogram[i/fracc]/(double)max);
            histImage.setPixel(i,h,qRgb(255,255,255));
            for(int j=h+1;j<height;j++)
                histImage.setPixel(i,j,qRgb(128,128,128));

    }

    return histImage;
}
 void HistogramGray::calcCDF()
{
     for(int i=0;i<256;i++)
     {
         CDF[i]=0;
     }
    for(int i=0;i<256;i++)
    {
        for(int j=0;j<=i;j++)
        {
            CDF[i]=histogram[j]+CDF[i];
        }
    }

}
 unsigned long * HistogramGray::getCDF()
 {
     return this->CDF;
 }



